package com.shunde.xunjian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.shunde.xunjian.entity.ComplaintRegistration;
import com.shunde.xunjian.entity.ReportOrders;
import com.shunde.xunjian.mapper.dbnetworkshunde.ComplaintRegistrationMapper;
import com.shunde.xunjian.mapper.dbnetworkshunde.ReportOrdersMapper;
import com.shunde.xunjian.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Service
public class ReportOrdersServiceImpl {

    @Autowired
    private ReportOrdersMapper reportOrdersMapper;

    @Autowired
    private ComplaintRegistrationMapper complaintRegistrationMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

    public Object ReportOrdersNums() {
        QueryWrapper<ReportOrders> reportOrdersQueryWrapper = new QueryWrapper<>();
        Integer allReportOrdersNums = reportOrdersMapper.selectCount(reportOrdersQueryWrapper);
        reportOrdersQueryWrapper.eq("order_status", "待分配");
        Integer toBeDistributedReportOrdersNums = reportOrdersMapper.selectCount(reportOrdersQueryWrapper);
        reportOrdersQueryWrapper.clear();
        reportOrdersQueryWrapper.eq("order_status", "待处理");
        Integer tobeProcessedReportOrdersNums = reportOrdersMapper.selectCount(reportOrdersQueryWrapper);
        reportOrdersQueryWrapper.clear();
        reportOrdersQueryWrapper.eq("order_status", "已处理");
        Integer processedReportOrdersNums = reportOrdersMapper.selectCount(reportOrdersQueryWrapper);
        reportOrdersQueryWrapper.clear();
        reportOrdersQueryWrapper.eq("order_status", "已核查");
        Integer checkedReportOrdersNums = reportOrdersMapper.selectCount(reportOrdersQueryWrapper);
        reportOrdersQueryWrapper.clear();
        Map<String, Integer> reportOrdersNumsMap = new HashMap<>();
        reportOrdersNumsMap.put("allReportOrdersNums", allReportOrdersNums);
        reportOrdersNumsMap.put("toBeDistributedReportOrdersNums", toBeDistributedReportOrdersNums);
        reportOrdersNumsMap.put("tobeProcessedReportOrdersNums", tobeProcessedReportOrdersNums);
        reportOrdersNumsMap.put("processedReportOrdersNums", processedReportOrdersNums);
        reportOrdersNumsMap.put("checkedReportOrdersNums", checkedReportOrdersNums);
        return reportOrdersNumsMap;
    }

    public Object ReportOrdersResults(String reportOrdersStr, Integer current, Integer pageSize) throws ParseException {
        ReportOrders reportOrders = new Gson().fromJson(reportOrdersStr, ReportOrders.class);
        QueryWrapper<ReportOrders> reportOrdersQueryWrapper = new QueryWrapper<>();
        IPage<ReportOrders> page = new Page<>(current, pageSize);
        if (reportOrdersStr != null) {
            if (reportOrders.getOrderType() != null) {
                reportOrdersQueryWrapper.eq("order_type", reportOrders.getOrderType());
            }
            if (reportOrders.getOrderStatus() != null) {
                reportOrdersQueryWrapper.eq("order_status", reportOrders.getOrderStatus());
            }
            if (reportOrders.getOrderSource() != null) {
                reportOrdersQueryWrapper.eq("order_source", reportOrders.getOrderSource());
            }
            if (reportOrders.getFacilityProblem() != null) {
                reportOrdersQueryWrapper.eq("facility_problem", reportOrders.getFacilityProblem());
            }
            if (reportOrders.getHandler() != null) {
                reportOrdersQueryWrapper.eq("handler", reportOrders.getHandler());
            }
            if (reportOrders.getSTime() != null && reportOrders.getETime() != null) {
                reportOrdersQueryWrapper.between("report_time", sdf.parse(sdf.format(sdf1.parse(reportOrders.getSTime()))), sdf.parse(sdf.format(sdf1.parse(reportOrders.getETime()))));
            }
        }
        IPage<ReportOrders> reportOrdersIPage = reportOrdersMapper.selectPage(page, reportOrdersQueryWrapper.orderByAsc("report_time"));
        return reportOrdersIPage;
    }

    public Integer dispatchReportOrders(String reportOrdersStr) {
        ReportOrders reportOrders = new Gson().fromJson(reportOrdersStr, ReportOrders.class);
        Integer result = 0;
        if (!Utils.isEntityEmpty(reportOrders)) {
            reportOrders.setOrderId(reportOrders.getOrderId());
            reportOrders.setOrderStatus("待处理");
            result = reportOrdersMapper.updateById(reportOrders);
        }
        return result;
    }

    //上报工单
    public Integer ReportOrdersUpload(String reportOrdersStr) throws ParseException {
        ReportOrders reportOrders = new Gson().fromJson(reportOrdersStr, ReportOrders.class);
        Integer result = 0;
        if (!Utils.isEntityEmpty(reportOrders)) {
            reportOrders.setOrderStatus("待分配");
            result = reportOrdersMapper.insert(reportOrders);
        }
        return result;
    }

    //APP处理上报
    public Integer dealReportOrdersUpload(String reportOrdersStr) {
        ReportOrders reportOrders = new Gson().fromJson(reportOrdersStr, ReportOrders.class);
        Integer result = 0;
        if (!Utils.isEntityEmpty(reportOrders)) {
            reportOrders.setOrderStatus("已处理");
            result = reportOrdersMapper.updateById(reportOrders);
        }
        return result;
    }

    public Object appOrderNums(String reportOrdersStrMatinance,String reportOrdersStrClean, String complaintRegistrationStr) {
        ReportOrders reportOrdersMatinance = new Gson().fromJson(reportOrdersStrMatinance, ReportOrders.class);
        ReportOrders reportOrdersClean = new Gson().fromJson(reportOrdersStrClean, ReportOrders.class);
        ComplaintRegistration complaintRegistration = new Gson().fromJson(complaintRegistrationStr, ComplaintRegistration.class);
        QueryWrapper<ReportOrders> reportOrdersQueryWrapperMatinance = new QueryWrapper<>();
        QueryWrapper<ReportOrders> reportOrdersQueryWrapperClean = new QueryWrapper<>();
        reportOrdersQueryWrapperMatinance.eq("order_type", "维修");
        reportOrdersQueryWrapperClean.eq("order_type", "清疏");

        if (reportOrdersStrMatinance != null && !reportOrdersStrMatinance.isEmpty()) {
            if (reportOrdersMatinance.getOrderStatus() != null) {
                reportOrdersQueryWrapperMatinance.eq("order_status", reportOrdersMatinance.getOrderStatus());
            }
            if (reportOrdersMatinance.getOrderType() != null) {
                reportOrdersQueryWrapperMatinance.eq("order_type", reportOrdersMatinance.getOrderType());
            }
            if (reportOrdersMatinance.getProcessingMethod() != null) {
                reportOrdersQueryWrapperMatinance.eq("processing_method", reportOrdersMatinance.getProcessingMethod());
            }
            if (reportOrdersMatinance.getReporter() != null) {
                reportOrdersQueryWrapperMatinance.eq("reporter", reportOrdersMatinance.getReporter());
            }
            if (reportOrdersMatinance.getOrderSource() != null) {
                reportOrdersQueryWrapperMatinance.eq("order_source", reportOrdersMatinance.getOrderSource());

            }
            if (reportOrdersMatinance.getArea() != null) {
                reportOrdersQueryWrapperMatinance.eq("area", reportOrdersMatinance.getArea());
            }
            if (reportOrdersMatinance.getTownStreet() != null) {
                reportOrdersQueryWrapperMatinance.eq("town_street", reportOrdersMatinance.getTownStreet());
            }
            if (reportOrdersMatinance.getHandler() != null) {
                reportOrdersQueryWrapperMatinance.eq("handler", reportOrdersMatinance.getHandler());
            }
        }
        if (reportOrdersStrClean != null && !reportOrdersStrClean.isEmpty()) {
            if (reportOrdersClean.getOrderStatus() != null) {
                reportOrdersQueryWrapperClean.eq("order_status", reportOrdersClean.getOrderStatus());
            }
            if (reportOrdersClean.getOrderType() != null) {
                reportOrdersQueryWrapperClean.eq("order_type", reportOrdersClean.getOrderType());
            }
            if (reportOrdersClean.getProcessingMethod() != null) {
                reportOrdersQueryWrapperClean.eq("processing_method", reportOrdersClean.getProcessingMethod());
            }
            if (reportOrdersClean.getReporter() != null) {
                reportOrdersQueryWrapperClean.eq("reporter", reportOrdersClean.getReporter());
            }
            if (reportOrdersClean.getOrderSource() != null) {
                reportOrdersQueryWrapperClean.eq("order_source", reportOrdersClean.getOrderSource());
            }
            if (reportOrdersClean.getArea() != null) {
                reportOrdersQueryWrapperClean.eq("area", reportOrdersClean.getArea());
            }
            if (reportOrdersClean.getTownStreet() != null) {
                reportOrdersQueryWrapperClean.eq("town_street", reportOrdersClean.getTownStreet());
            }
            if (reportOrdersClean.getHandler() != null) {
                reportOrdersQueryWrapperClean.eq("handler", reportOrdersClean.getHandler());
            }
        }
        //清疏工单数量
        Integer maintenanceNums = reportOrdersMapper.selectCount(reportOrdersQueryWrapperMatinance);
        //清疏工单数量
        Integer clearOrderNums = reportOrdersMapper.selectCount(reportOrdersQueryWrapperClean);
        //投诉核查数量
        QueryWrapper<ComplaintRegistration> complaintRegistrationQueryWrapper = new QueryWrapper<>();
        if (complaintRegistrationStr != null && !complaintRegistrationStr.isEmpty()) {
            if (complaintRegistration.getComplaintSource() != null) {
                complaintRegistrationQueryWrapper.eq("complaint_source", complaintRegistration.getComplaintSource());
            }
            if (complaintRegistration.getArea() != null) {
                complaintRegistrationQueryWrapper.eq("area", complaintRegistration.getArea());
            }
            if (complaintRegistration.getTownStreet() != null) {
                complaintRegistrationQueryWrapper.eq("town_street", complaintRegistration.getTownStreet());
            }
            if (complaintRegistration.getReviewStatus() != null) {
                complaintRegistrationQueryWrapper.eq("review_status", complaintRegistration.getReviewStatus());
            }
        }
        Integer complaintRegistrationNums = complaintRegistrationMapper.selectCount(complaintRegistrationQueryWrapper);
        Map<String, Integer> reportOrdersNumsMap = new HashMap<>();
        reportOrdersNumsMap.put("maintenanceNums", maintenanceNums);
        reportOrdersNumsMap.put("clearOrderNums", clearOrderNums);
        reportOrdersNumsMap.put("complaintRegistrationNums", complaintRegistrationNums);
        return reportOrdersNumsMap;
    }
}
